//
//  UIView+YQTool.m
//  PensionDoctor
//
//  Created by tbb on 2019/6/28.
//  Copyright © 2019 YQ. All rights reserved.
//

#import "UIView+YQTool.h"
#import <objc/runtime.h>
@interface UIView()
@property (nonatomic, copy) ActionBlock action;

@end

@implementation UIView (YQTool)

static void *actionKey = &actionKey;

-(void)setAction:(ActionBlock)action{
    objc_setAssociatedObject(self, &actionKey, action, OBJC_ASSOCIATION_COPY);
}

-(ActionBlock)action{
    return objc_getAssociatedObject(self, &actionKey);
}

-(void)addTapAction:(ActionBlock)action{
    self.userInteractionEnabled = YES;
    self.action = action;
    UITapGestureRecognizer * g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapGestureRecognizer:)];
    [self addGestureRecognizer:g];
}

-(void)action_tapGestureRecognizer:(id)sender{
    if (self.action) {
        self.action(sender);
    }
}

+(void)roundedSetView:(UIView *)View cornerRadius:(CGFloat)radius Type:(kRoundedSetViewType)type{
    UIBezierPath *maskPath = nil;
    if (type == kRoundedSetViewTypeLTB) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:View.bounds   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft  cornerRadii:CGSizeMake(radius, radius)];
    } else if (type == kRoundedSetViewTypeRTB){
        maskPath = [UIBezierPath bezierPathWithRoundedRect:View.bounds   byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight  cornerRadii:CGSizeMake(radius, radius)];
    }else if (type == kRoundedSetViewTypeTRL){
        maskPath = [UIBezierPath bezierPathWithRoundedRect:View.bounds   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight  cornerRadii:CGSizeMake(radius, radius)];
    }else if (type == kRoundedSetViewTypeBRL){
        maskPath = [UIBezierPath bezierPathWithRoundedRect:View.bounds   byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight  cornerRadii:CGSizeMake(radius, radius)];
    }
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = View.bounds;
    maskLayer.path = maskPath.CGPath;
    
    View.layer.mask = maskLayer;
}

/*
 周边加阴影，并且同时圆角
 */
+ (void)addShadowToView:(UIView *)view
            withOpacity:(float)shadowOpacity
           shadowRadius:(CGFloat)shadowRadius
        andCornerRadius:(CGFloat)cornerRadius
{
    //////// shadow /////////
    CALayer *shadowLayer = [CALayer layer];
    shadowLayer.frame = view.layer.frame;
    
    shadowLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    shadowLayer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    shadowLayer.shadowOpacity = shadowOpacity;//0.8;//阴影透明度，默认0
    shadowLayer.shadowRadius = shadowRadius;//8;//阴影半径，默认3
    
    //路径阴影
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    float width = shadowLayer.bounds.size.width;
    float height = shadowLayer.bounds.size.height;
    float x = shadowLayer.bounds.origin.x;
    float y = shadowLayer.bounds.origin.y;
    
    CGPoint topLeft      = shadowLayer.bounds.origin;
    CGPoint topRight     = CGPointMake(x + width, y);
    CGPoint bottomRight  = CGPointMake(x + width, y + height);
    CGPoint bottomLeft   = CGPointMake(x, y + height);
    
    CGFloat offset = -1.f;
    [path moveToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    [path addArcWithCenter:CGPointMake(topLeft.x + cornerRadius, topLeft.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI endAngle:M_PI_2 * 3 clockwise:YES];
    [path addLineToPoint:CGPointMake(topRight.x - cornerRadius, topRight.y - offset)];
    [path addArcWithCenter:CGPointMake(topRight.x - cornerRadius, topRight.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 * 3 endAngle:M_PI * 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomRight.x + offset, bottomRight.y - cornerRadius)];
    [path addArcWithCenter:CGPointMake(bottomRight.x - cornerRadius, bottomRight.y - cornerRadius) radius:(cornerRadius + offset) startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y + offset)];
    [path addArcWithCenter:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y - cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    
    //设置阴影路径
    shadowLayer.shadowPath = path.CGPath;
    
    //////// cornerRadius /////////
    view.layer.cornerRadius = cornerRadius;
    view.layer.masksToBounds = YES;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    [view.superview.layer insertSublayer:shadowLayer below:view.layer];
}
@end
